package com.xiaxiaoge.tutorialmod.items.vehicle;

import com.xiaxiaoge.tutorialmod.entity.custom.VictoriousSwiftTwoEntity;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;

import java.util.List;
import java.util.function.Predicate;

/**
 * VictorySwiftTwoItem 实体类
 * 拿在手里时的状态
 * @author xsl
 * @version 1.0
 * @Date 2024/8/17 14:13
 */
public class VictorySwiftTwoItem extends Item {

    private static final Predicate<Entity> ENTITY_PREDICATE = EntitySelector.NO_SPECTATORS.and(Entity::isPickable);


    public VictorySwiftTwoItem(Properties p_41383_) {
        super(p_41383_);
    }

    /**
     * 使用时的方法
     */
    @Override
    public InteractionResultHolder<ItemStack> use(Level p_41432_, Player p_41433_, InteractionHand p_41434_) {
        ItemStack itemstack = p_41433_.getItemInHand(p_41434_);
        HitResult hitresult = getPlayerPOVHitResult(p_41432_, p_41433_, ClipContext.Fluid.ANY);
        if (hitresult.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.pass(itemstack);
        } else {
            Vec3 vec3 = p_41433_.getViewVector(1.0F);
            double d0 = 5.0D;
            List<Entity> list = p_41432_.getEntities(p_41433_, p_41433_.getBoundingBox().expandTowards(vec3.scale(5.0D)).inflate(1.0D), ENTITY_PREDICATE);
            if (!list.isEmpty()) {
                Vec3 vec31 = p_41433_.getEyePosition();

                for(Entity entity : list) {
                    AABB aabb = entity.getBoundingBox().inflate((double)entity.getPickRadius());
                    if (aabb.contains(vec31)) {
                        return InteractionResultHolder.pass(itemstack);
                    }
                }
            }

            if (hitresult.getType() == HitResult.Type.BLOCK) {
                VictoriousSwiftTwoEntity victoriousSwiftTwoEntity = new VictoriousSwiftTwoEntity(p_41432_, hitresult.getLocation().x, hitresult.getLocation().y, hitresult.getLocation().z);
                victoriousSwiftTwoEntity.setYRot(p_41433_.getYRot());
                if (!p_41432_.noCollision(victoriousSwiftTwoEntity, victoriousSwiftTwoEntity.getBoundingBox())) {
                    return InteractionResultHolder.fail(itemstack);
                } else {
                    if (!p_41432_.isClientSide) {
                        p_41432_.addFreshEntity(victoriousSwiftTwoEntity);
                        p_41432_.gameEvent(p_41433_, GameEvent.ENTITY_PLACE, hitresult.getLocation());
                        if (!p_41433_.getAbilities().instabuild) {
                            itemstack.shrink(1);
                        }
                    }

                    p_41433_.awardStat(Stats.ITEM_USED.get(this));
                    return InteractionResultHolder.sidedSuccess(itemstack, p_41432_.isClientSide());
                }
            } else {
                return InteractionResultHolder.pass(itemstack);
            }
        }
    }
}
